package com.example.studyllm.service.base;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class BaseServiceImpl<M extends BaseMapper<T>, T extends Model> extends ServiceImpl<M, T> implements IBaseService<T> {
    private static final Logger log = LoggerFactory.getLogger(BaseServiceImpl.class);

    public BaseServiceImpl() {
    }

    public boolean save(T entity) {
        return super.save(entity);
    }

    public boolean updateById(T entity) {
        return super.updateById(entity);
    }

    public void deleteByIds(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        this.removeByIds(idList);
    }


    public List<T> getListByField(SFunction<T, ?> field, Object value) {
        if (ObjectUtil.isNull(value)) {
            return null;
        } else {
            LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(field, value);
            return this.list(queryWrapper);
        }
    }

    public List<T> getListByFieldStrList(SFunction<T, ?> field, Collection<String> value) {
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(field, value);
        return this.list(queryWrapper);
    }

    public List<T> getListByFieldLongList(SFunction<T, ?> field, Collection<Long> value) {
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(field, value);
        return this.list(queryWrapper);
    }

    public void deleteByIdList(List<Long> idList) {
        this.removeByIds(idList);
    }




}
